﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB.Mechanical;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace HangerSupport
{
	public partial class DlgDuctWeightCal : System.Windows.Forms.Form
	{
		public DlgDuctWeightCal(List<SystemLine> _lines)
		{
			this.InitializeComponent();
			YJKProcessTools hyprocessTools = new YJKProcessTools();
			hyprocessTools.ShowInTaskbarChange(this);
			this.userSetting = new UserSetting();
			this.lines = _lines;
			base.Icon = Resource.YArchitechIcon;
			this.bindGrd();
		}

		private void bindGrd()
		{
			List<DuctWeightData> list = new List<DuctWeightData>();
			foreach (SystemLine systemLine in this.lines)
			{
				double num = 0.0;
				double num2 = 0.0;
				double num3 = 0.0;
				foreach (Duct item in systemLine.Pipes.Cast<Duct>().ToList<Duct>())
				{
					double ductThinkness = 0.0;
					double width = 0.0;
					double height = 0.0;
					double length = 0.0;
					double ductWeight = this.getDuctWeight(item, ref ductThinkness, ref width, ref height, ref length);
					double fittingWeight = this.getFittingWeight(width, height);
					double warmWeight = this.getWarmWeight(item, ductThinkness, width, height, length);
					num += ductWeight;
					num2 += fittingWeight;
					num3 += warmWeight;
					this.totalDuctWeight += ductWeight;
					this.totalFittingWeight += fittingWeight;
					this.totalWarmWeight += warmWeight;
				}
				DuctWeightData item2 = new DuctWeightData
				{
					SystemName = systemLine.SystemName,
					DuctWeight = string.Format("{0:F}", Math.Round(num, 2)),
					FittingWeight = string.Format("{0:F}", Math.Round(num2, 2)),
					WarmWeight = string.Format("{0:F}", Math.Round(num3, 2))
				};
				list.Add(item2);
			}
			this.dataGridView1.DataSource = list;
			this.setDefault();
			this.calTotal();
			this.isInt = false;
		}

		private void setDefault()
		{
			this.hyTextBox1.Text = ((this.userSetting.GetValue("DuctWeightCal", "addWeight") == "") ? "0" : this.userSetting.GetValue("DuctWeightCal", "addWeight"));
			this.hyTextBox3.Text = ((this.userSetting.GetValue("DuctWeightCal", "addRatio") == "") ? "10" : this.userSetting.GetValue("DuctWeightCal", "addRatio"));
			this.hyTextBox2.Text = ((this.userSetting.GetValue("DuctWeightCal", "itemizeRatio") == "") ? "1.35" : this.userSetting.GetValue("DuctWeightCal", "itemizeRatio"));
		}

		private void calTotal()
		{
			this.addWeight = ((this.hyTextBox1.Text.Trim() == "") ? 0.0 : Convert.ToDouble(this.hyTextBox1.Text));
			this.addRatio = ((this.hyTextBox3.Text.Trim() == "") ? 0.0 : Convert.ToDouble(this.hyTextBox3.Text));
			this.itemizeRatio = ((this.hyTextBox2.Text.Trim() == "") ? 0.0 : Convert.ToDouble(this.hyTextBox2.Text));
			this.userSetting.WriteValue("DuctWeightCal", "addWeight", this.addWeight.ToString());
			this.userSetting.WriteValue("DuctWeightCal", "addRatio", this.addRatio.ToString());
			this.userSetting.WriteValue("DuctWeightCal", "itemizeRatio", this.itemizeRatio.ToString());
			this.hyTextBox4.Text = Math.Round((this.totalDuctWeight + this.totalWarmWeight + this.totalFittingWeight + this.addWeight) * (1.0 + this.addRatio / 100.0) * 9.8 * this.itemizeRatio, 2).ToString();
		}

		private double getFittingWeight(double width, double height)
		{
			return 2.0 * (width / 1000.0 + height / 1000.0) * 2.0 * 1.05 * 2.422;
		}

		private double getWarmWeight(Duct item, double DuctThinkness, double width, double height, double length)
		{
			string text = item.GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_REFERENCE_INSULATION_TYPE).AsString();
			bool flag = text == "" || text == null;
			double result;
			if (flag)
			{
				result = 0.0;
			}
			else
			{
				double num = 70.0;
				bool flag2 = this.warmList.ContainsKey(text);
				if (flag2)
				{
					num = this.warmList[text];
				}
				double num2 = NumbericTools.feetTomm(item.GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_REFERENCE_INSULATION_THICKNESS).AsDouble());
				result = 2.0 * (width / 1000.0 + height / 1000.0 + DuctThinkness * 2.0 / 1000.0) * length / 1000.0 * num2 / 1000.0 * num;
			}
			return result;
		}

		private double getDuctWeight(Duct item, ref double thinkness, ref double width, ref double height, ref double length)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(item);
			width = NumbericTools.feetTomm(hypiping.GetWeightOrDiameter());
			height = NumbericTools.feetTomm(hypiping.GetHeightOrDiameter());
			length = NumbericTools.feetTomm(item.GetParameter(Autodesk.Revit.DB.BuiltInParameter.CURVE_ELEM_LENGTH).AsDouble());
			double num = 7850.0;
			thinkness = this.getDuctThinkness((width > height) ? width : height);
			return 2.0 * (width / 1000.0 + height / 1000.0) * length / 1000.0 * thinkness / 1000.0 * num;
		}

		private double getDuctThinkness(double sideLength)
		{
			bool flag = sideLength <= 320.0;
			double result;
			if (flag)
			{
				result = 0.5;
			}
			else
			{
				bool flag2 = sideLength > 320.0 && sideLength <= 450.0;
				if (flag2)
				{
					result = 0.6;
				}
				else
				{
					bool flag3 = sideLength > 450.0 && sideLength <= 630.0;
					if (flag3)
					{
						result = 0.6;
					}
					else
					{
						bool flag4 = sideLength > 630.0 && sideLength <= 1000.0;
						if (flag4)
						{
							result = 0.75;
						}
						else
						{
							bool flag5 = sideLength > 1000.0 && sideLength <= 1250.0;
							if (flag5)
							{
								result = 1.0;
							}
							else
							{
								bool flag6 = sideLength > 1250.0 && sideLength <= 2000.0;
								if (flag6)
								{
									result = 1.0;
								}
								else
								{
									bool flag7 = sideLength > 2000.0 && sideLength <= 4000.0;
									if (flag7)
									{
										result = 1.2;
									}
									else
									{
										result = 0.0;
									}
								}
							}
						}
					}
				}
			}
			return result;
		}

		private double calDuctWeight(double thickness, Duct duct)
		{
            return thickness * NumbericTools.feetTomm(duct.GetCurveLength((Autodesk.Revit.DB.BuiltInParameter)(-1004005))) / 1000.0;
		}

		private bool isWeight(double formatDim, double dim)
		{
			bool flag = Math.Abs(dim - formatDim) < 0.0001;
			bool result;
			if (flag)
			{
				result = true;
			}
			else
			{
				bool flag2 = dim < formatDim;
				result = flag2;
			}
			return result;
		}

		private void button2_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void hyTextBox1_TextChanged(object sender, EventArgs e)
		{
			bool flag = !this.isInt;
			if (flag)
			{
				this.calTotal();
			}
		}

		private List<SystemLine> lines = null;

		private double addWeight = 0.0;

		private double addRatio = 10.0;

		private double itemizeRatio = 1.35;

		private double totalDuctWeight = 0.0;

		private double totalWarmWeight = 0.0;

		private double totalFittingWeight = 0.0;

		private bool isInt = true;

		private UserSetting userSetting = null;

		private Dictionary<string, double> warmList = new Dictionary<string, double>
		{
			{
				"玻璃棉制品",
				90.0
			},
			{
				"超细玻璃棉制品",
				80.0
			},
			{
				"泡沫橡塑制品(PVC/NBR)",
				95.0
			},
			{
				"酚醛泡沫制品(PF)",
				70.0
			},
			{
				"复合硅酸盐制品",
				160.0
			},
			{
				"聚氨酯泡沫制品",
				60.0
			},
			{
				"聚苯乙烯泡沫制品",
				30.0
			},
			{
				"聚乙烯泡沫制品(PEF)",
				50.0
			},
			{
				"岩棉制品",
				200.0
			},
			{
				"泡沫玻璃制品",
				180.0
			},
			{
				"硅酸铝制品",
				192.0
			},
			{
				"微孔硅酸钙制品",
				220.0
			},
			{
				"憎水珍珠岩制品",
				200.0
			}
		};
	}
}
